<template>
	<view>
		<det_bar title='详情页'></det_bar>
		<view style="height: 56px;"></view>
		<view class="dettop">
			<dl class="topdl">
				<dt>
					<image :src="itemDetails.logo" mode=""></image>
				</dt>
				<dd class="headers">
					<h3><span id="pin">品牌</span>{{itemDetails.shopname}}</h3>
					<p><span>月售{{itemDetails.MonthlySales}}单</span><span>{{itemDetails.content}}</span></p>
				</dd>
			</dl>
		</view>
		<u-tabs :list="list" @click="click"></u-tabs>
		<!-- 根据选项对象的信息来展示不同的内容 -->

		<div v-if="currentIndex === 0" style="border-top: 1px solid #ccc;">
			<div v-if="list1.length === 0">
				<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png">
				</u-empty>
			</div>
			<!-- 点餐module-->
			<div v-else>
				<dl class="menu" v-for="(item,index) in list1" :key="index">
					<view class="shop_item" @click="headleSelectProduct(item)">
						<dt><img :src="item.foodpic" alt="" /></dt>
						<dd>
							<h3>{{item.foodname}}</h3>
							<p style="color: #666;font-size: 12px;">{{item.descr}}</p>
							<p style="color: red;">{{item.price}}￥</p>
						</dd>
					</view>
				</dl>
			</div>
		</div>

		<div v-else style="border-top: 1px solid #ccc;">
			<!-- 评论module-->
			<div v-if="list2.length === 0">
				<u-empty mode="comment" icon="http://cdn.uviewui.com/uview/empty/comment.png">
				</u-empty>
			</div>
			<div v-else>
				<dl class="menu" v-for="(item,index) in list2" :key="index">
					<dt><img :src="item.userpic" alt="" />
					</dt>
					<dd>
						<h3>{{item.comments_user}}</h3>
						<p style="color: #666;font-size: 12px;">{{item.content}}</p>
						<p style="color: #666;font-size: 12px;">{{item.comments_time}}</p>
					</dd>
				</dl>
			</div>
		</div>
		<u-popup :show="show" mode="bottom" @close="close">
			<view>
				<view class="headleCartTop">
					<view>购物车</view>
					<view style="color: #0e932e;" @click="clearCart">清空</view>
				</view>
				<view v-for="(item,index) in orderList" :key="index">
					<view class="clearingGoods">
						<u-row customStyle="margin-bottom: 10px">
							<u-col span="6">
								<view class="title">{{item.foodname}}</view>
							</u-col>
							<u-col span="1.5">
								<view style="text-align: right; color: #aa0000;"><b
										style="font-size: 18px;">{{item.price*item.num}}</b>¥</view>
							</u-col>
							<u-col span=".5" />
							<u-col span="4">
								<u-number-box :name="item.id" model={item.num} @change="valChange"></u-number-box>
							</u-col>
						</u-row>
					</view>
				</view>
			</view>
			<view style="height: 100px;" />
			<div class="foot">
				<view class="shoppingCart" @click="headleCartPopup">
					<view class="car"><img src="static/购物车.png" alt="" /><span>{{totality}}</span></view>
				</view>
				<b class="Total_Price">{{totalPrice}}￥</b>
				<div v-show="this.totalPrice < 10" class="song" @click="settlement">还差{{10 - totalPrice}}元起送</div>
				<div v-show="this.totalPrice >= 10" class="satisfy" @click="settlement">支&nbsp;付</div>
			</div>
		</u-popup>
		<div class="foot">
			<view class="shoppingCart" @click="headleCartPopup">
				<view class="car"><img src="static/购物车.png" alt="" /><span>{{totality}}</span></view>
			</view>
			<b class="Total_Price">{{totalPrice}}￥</b>
			<div v-show="this.totalPrice < 10" class="song" @click="settlement">还差{{10 - totalPrice}}元起送</div>
			<div v-show="this.totalPrice >= 10" class="satisfy" @click="settlement">支&nbsp;付</div>
		</div>
	</view>
</template>

<script>
	import {
		ipconfig
	} from '../../ipconfig.js';

	export default {
		data() {
			return {
				itemDetails: {},
				list: [{
					name: '点餐'
				}, {
					name: '评价'
				}],
				currentTab: null, // 用于保存当前选中的选项对象
				currentIndex: 0, // 默认选中第一个选项
				list1: [],
				list2: [],
				type: "error",
				value: 20,
				orderList: [],
				show: false
			}
		},
		onLoad(e) {
			console.log(e.res, '详情页打印');
			this.itemDetails = JSON.parse(e.res);
			const id = this.itemDetails.id;
			console.log(id);
			let _this = this;
			uni.request({
				url: `http://${ipconfig}:8002/admin/vueshopgoods?id=${id}`,
				method: 'get',
				success: (res) => {
					_this.list1 = res.data
				}
			});
			uni.request({
				url: `http://${ipconfig}:8002/admin/vuecomments?id=${id}`,
				method: 'get',
				success: (res) => {
					_this.list2 = res.data
				}
			});
		},
		computed: {
			// 总数计算
			totality() {
				let total = this.orderList.reduce((accumulator, currentValue) => {
					return accumulator + currentValue.num;
				}, 0);
				console.log(total);
				return total;
			},
			// 总价计算
			totalPrice() {
				let total = 0;
				this.orderList.forEach(item => {
					total += item.price * item.num;
				});
				return total.toFixed(2);
			}
		},

		methods: {
			// tab切换
			click(tab) {
				console.log(tab.index, '索引');
				this.currentIndex = tab.index;
			},
			// 购物车弹出层
			headleCartPopup() {
				this.show = true
			},
			// 点击空白关闭
			close() {
				this.show = false
			},
			// 添加购物车
			headleSelectProduct(goods) {
				if (this.orderList.length === 0) {
					goods.num = 1;
					this.orderList.push(goods);
					console.log(this.orderList, '加');
				} else {
					let found = false;
					this.orderList.forEach((item) => {
						if (item.id === goods.id) {
							if (item.num >= item.maxcount) {
								uni.showToast({
									title: `此商品最多可购买${item.maxcount}件`,
									duration: 2000
								});
								found = true;
							} else {
								item.num++;
								this.$set(this.orderList, this.orderList.length - 1,
									goods); // 使用$this.$set来触发对象属性的变化
							}
							found = true;
						}
					});
					if (!found) {
						goods.num = 1;
						this.orderList.push(goods);
					}
				}

			},
			// 商品数量更新
			valChange(e) {
				this.orderList.forEach((item)=>{
					if(item.id === e.name){
						item.num = e.value
					}
				})
			},
			// 结算的点击事件
			settlement() {
				this.$store.commit('addShop', this.orderList)
				uni.reLaunch({
					url: '/pages/clearing/clearing',
				});
			},
			// 清空购物车
			clearCart(){
				this.orderList = []
				this.$store.commit('addShop', this.orderList)
				this.show = false
			}
		}
	};
</script>

<style scoped lang="scss">
	.dettop {
		height: 140px;
		padding-top: 10px;
	}

	.topdl {
		text-align: center;

		dt {
			image {
				width: 70px;
				height: 70px;
				border-radius: 5px;
			}
		}

		.headers {
			#pin {
				display: inline-block;
				color: #6A3709;
				font-size: 12px;
				width: 40px;
				height: 20px;
				line-height: 20px;
				background: #FFEF08;
				border-radius: 5px;
				margin-right: 10px;
			}

			h3 {
				color: #333333;
				font-size: 18px;
			}

			p {
				font-size: 12px;

				span {
					padding: 10px;
				}
			}
		}
	}

	.menu {

		.shop_item {
			width: 100%;
			padding: 5px;
			display: flex;
			align-items: center;
			border-bottom: 1px solid #ccc;

			dt {
				img {
					width: 75px;
					height: 75px;
					border-radius: 5px;
					margin: 5px 0 0 5px;
				}
			}

			dd {
				width: 100%;
				margin-left: 10px;
			}
		}
	}

	.foot {
		width: 100%;
		height: 50px;
		background: #141D27;
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.shoppingCart {
			width: 60px;
			height: 60px;
			border-radius: 50%;
			background-color: #141D27;
			position: fixed;
			bottom: 10px;
			left: 10px;

			.car {
				width: 50px;
				height: 50px;
				border-radius: 50%;
				background: #FFF;
				display: flex;
				justify-content: center;
				align-items: center;
				margin: 5px;

				img {
					width: 25px;
					height: 25px;
				}

				span {
					width: 16px;
					height: 16px;
					border-radius: 50%;
					background: red;
					position: absolute;
					display: inline-block;
					top: 0;
					right: 5px;
					color: white;
					text-align: center;
					line-height: 16px;
					font-size: 12px;
				}
			}

		}

		.Total_Price {
			color: white;
			position: absolute;
			left: 25%;
		}

		.song {
			width: 120px;
			height: 40px;
			background: #2B333B;
			color: #666;
			text-align: center;
			line-height: 40px;
			position: fixed;
			bottom: 5px;
			right: 10px;
			border-radius: 5px;
		}

		.satisfy {
			width: 120px;
			height: 40px;
			background: #FFFFFF;
			font-weight: bold;
			color: #2B333B;
			text-align: center;
			line-height: 40px;
			position: fixed;
			bottom: 5px;
			right: 10px;
			border-radius: 5px;
		}
	}

	.headleCartTop {
		width: 100%;
		height: 40px;
		background-color: #CCC;
		display: flex;
		align-items: center;
		justify-content: space-around;
	}
	
	.clearingGoods {
		width: 100%;
		margin-top: 20px;
		border-bottom: solid 1px #ccc;
	}
	
</style>